package ui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.geom.RoundRectangle2D;
import ui.listeners.CheckerBoardCanvasMouseListener;

/**
 * Specialization of the AWT Canvas class. This can be added to a
 * CheckerBoardWindowFrame instance to create the infamous checker board
 * pattern.
 * 
 * @author Team Titanium
 */
public class CheckerBoardCanvas extends Canvas {
	private final Color RED_PIECE_REGULAR = new Color(127, 0, 0);
	private final Color BLACK_PIECE_REGULAR = new Color(0, 0, 0);
	
	private String winner;

	/**
	 * Generated SVUID. This member is required by the Serializable parent
	 * class.
	 */
	private static final long serialVersionUID = 4550943293392280170L;

	/*
	 * private int[][] board = { {0,1,0,1,0,1,0,1}, {1,0,1,0,1,0,1,0},
	 * {0,1,0,1,0,1,0,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0},
	 * {3,0,3,0,3,0,3,0}, {0,3,0,3,0,3,0,3}, {3,0,3,0,3,0,3,0} };
	 */
	private int[][] board;

	public CheckerBoardCanvas() {
		this(null);
	}

	public CheckerBoardCanvas(GraphicsConfiguration gc) {
		super(gc);
		winner = null;
		this.addMouseListener(new CheckerBoardCanvasMouseListener());
		board = new int[8][8];
	}

	/**
	 * Paints the checker board pattern.
	 * 
	 * @param g
	 *            The graphics object that the checker board will be painted to.
	 */
	public void paint(Graphics g) {
		Dimension size = getSize();

		// Create a clipping mask to give the board
		// nice round corners.
		RoundRectangle2D clip = new RoundRectangle2D.Double(0, 0,
				size.getWidth(), size.getHeight(), 11, 11);
		g.setClip(clip);

		// We use Math.ceil() here because simply casting to
		// an int (without ceil()) will create rounding errors
		// which cause the checker squares to not fill the intended
		// area of the board.
		int sq_height = (int) Math.ceil(size.getHeight() / 8.0);
		int sq_width = (int) Math.ceil(size.getWidth() / 8.0);

		Color bg1 = new Color(255, 255, 255); // White
		// Color bg2 = new Color(255,0,0); // Red
		Color bg2 = new Color(22, 141, 255); // Blue

		int pattern = 0;
		
		if (winner != null) {
			g.setColor(new Color(255,0,0));
			g.setFont(new Font("Courier", Font.BOLD, 25));
			g.drawString(winner + " WINS!", 100, 100);
			return;
		}

		int i = 0;
		while (i < 8) {
			int j = 0;
			while (j < 8) {
				Color target_color = bg1;
				int sq_pos_x = sq_width * j;
				int sq_pos_y = sq_height * i;

				if (pattern % 2 == 1) {
					target_color = bg2;
				}

				g.setColor(target_color);
				g.fillRect(sq_pos_x, sq_pos_y, sq_width, sq_height);

				int piece_pos_diameter = (int) (sq_height * 0.8);
				int piece_pos_x = sq_pos_x
						+ ((int) Math.ceil((sq_width - piece_pos_diameter) / 2));
				int piece_pos_y = sq_pos_y
						+ ((int) Math
								.ceil((sq_height - piece_pos_diameter) / 2));

				if (board[i][j] != 0) {
					switch (board[i][j]) {
					// Red pieces
					case 1: // regular
					case 2: // king
						g.setColor(RED_PIECE_REGULAR);
						break;

					// Black pieces
					case 3: // regular
					case 4: // king
						g.setColor(BLACK_PIECE_REGULAR);
						break;
					}
					g.fillOval(piece_pos_x, piece_pos_y, piece_pos_diameter,
							piece_pos_diameter);

					// draw "K" for king if applicable
					if (board[i][j] % 2 == 0) {
						g.setColor(new Color(255, 255, 255));
						g.drawString("K", piece_pos_x + piece_pos_diameter / 2,
								piece_pos_y + piece_pos_diameter / 2);
					}
				}

				j++;
				pattern++;
			}
			pattern--;
			i++;
		}
	}

	/**
	 * Sets the internal board that this component uses to paint the pieces
	 * with.
	 * 
	 * @param newboard
	 *            A new, 8-by-8 array representing the board.
	 */
	public void setBoard(int[][] newboard) {
		board = newboard;
	}

	/**
	 * Determine what piece (if any) is at a given location on the board.
	 * 
	 * @param x
	 *            X position in an 8-by-8 array.
	 * @param y
	 *            Y position in an 8-by-8 array.
	 * @return 0 if no piece is there; 1 or 2 if a red piece is there; 3 or 4 if
	 *         a black piece is there.
	 */
	public int getPieceAt(int x, int y) {
		return board[y][x];
	}
	
	public void setGameWinner(String color) {
		
	}
}
